package Work4.CalculateArea;

import java.util.Scanner;
import java.util.ArrayList;

public class CalculateArea{
	public static void main(String[] args) {
		ArrayList<Shape> arr=new ArrayList<Shape>();
		boolean temp=false;
		Scanner sc= new Scanner(System.in);
		do {
			System.out.println("1. 添加圆形 2. 添加矩形 3. 计算所有面积 4. 退出");
			int choice=getint(sc);
			switch(choice) {
			case 1:
				System.out.println("输入半径:");
				double r=getdouble(sc);
				arr.add(new Circle(r));
				break;
			case 2:
				System.out.println("输入长和宽:");
				double l=getdouble(sc);
				double w=getdouble(sc);
				arr.add(new Rectangle(l,w));
				break;
			case 3:
				if(arr.size()>0) {
					for(int i=0;i<arr.size();i++) {
						System.out.println(arr.get(i).getShapename()+"面积:"+arr.get(i).CalculateArea());
					}
				}else {
					System.out.println("请输入图形数据");
				}
				break;
			case 4:
				temp=true;
				break;
			default:
				System.out.println("输入无效:");
			}
		}while(!temp);
	}
	public static int getint(Scanner sc) {
		while (!sc.hasNextInt()) {
		    System.out.println("输入无效，请输入一个整数:");
		    sc.next(); // 清除错误输入，避免死循环
		}
		return sc.nextInt();
	}
	public static double getdouble(Scanner sc) {
		while (!sc.hasNextDouble()) {
		    System.out.println("输入无效，请输入一个数:");
		    sc.next(); // 清除错误输入，避免死循环
		}
		return sc.nextDouble();
	}
}
